<?php include_once 'include/header.php'; ?>
<?php include_once 'postcontroller.php'; ?>
<?php include_once 'commentcontroller.php'; ?>
<div id="content">
    <?php include_once 'include/leftsidebar.php' ?>
    <div id="cmiddle">
        <div id="content-middle">           
            <?php
            foreach ($list_post_ct as $row) {                                                
                ?>
                <div class="content-post-comment" style="border-bottom: 1px solid #ccc;padding-bottom:8px;">
                    <div class="post-item">
                        <div style="float:left;margin:10px;">
                            <div dtyle="text-align:center"><img width="48px" height="48px" class="img-border" src="data/avatar/<?php echo $row["avatar"] ?>"/></div>
                            <div>
                                <?php if($row[5] == $_COOKIE["id"]){ ?>
                                <a class="link delete_post" pid="<?php echo $row[0] ?>"  style="color:#3d4fbb;cursor: pointer">Xóa bài này</a>
                                <?php } ?>
                            </div>
                        </div>
                        <div style="margin:10px;float:left;width:400px;word-wrap:break-word;">
                            <b style="color:#3d4fbb"><?php echo $row["fullname"] ?></b>
                            <br/>
                            <br/>
                            <span>
                                <?php echo $row["title"] ?>
                            </span>
                            <br/><br/>
                            <span style="margin-top:5px;color:#a11818">
                                <?php $date = date("F j, Y, g:i a",  strtotime("{$row["date_post"]}")); ?>
                                <?php echo $date ?> . <img src="Images/icon/comment.gif"/> 
                                <span class="comment-load" style="color:#3d4fbb"><?php echo $dem_comment ?></span>                                                                                                
                            </span>
                        </div>
                        <div style="clear: both"></div>
                    </div>
                    <div class="comment-content" style="width:522px;position: relative">
                        <img src="Images/icon/arrow.png" style="position: absolute;left:97px;top:-4px;"/>
                        <ul class="comment-item cm_<?php echo $row[0] ?>" style="margin-left:84px;">
                            
                            <?php
                            $list_comment = get_comment_chi_tiet($_GET["pid"]);
                            if(!empty($list_comment)){
                            foreach ($list_comment as $item) {
                                ?>
                                <li class="cm<?php echo $item[18] ?>">
                                    <?php if($item[0] == $_COOKIE["id"]){ ?>
                                    <span id="<?php echo $item[18] ?>" title="Xóa comment này" style="padding:1px;background: #fff;border:1px solid #ccc;cursor:pointer;" class="delete_comment">x</span>
                                    <?php } ?>
                                    <div style="float:left">
                                        <img width="28px" height="28px" class="img-border" src="data/avatar/<?php echo $item["avatar"] ?>"/>
                                    </div>
                                    <div style="float: left;padding-left:5px;width:380px;word-wrap:break-word">
                                        <b style="color:#286fc7"><?php echo $item["fullname"] ?></b>
                                        <?php echo $item["content"] ?>
                                        <br/><br/>
                                        <?php $date = date("F j, Y, g:i a",  strtotime("{$row["date_post"]}")); ?>
                                         <span style="color:#c76728">Đăng lúc <?php echo $date ?></span>
                                    </div>
                                    <div style="clear: both"></div>
                                </li>
                            <?php } ?>                
                                <?php } ?>
                        </ul>
                        <ul class="comment-item" style="margin-left:84px;">
                            <li class="comment-input_<?php echo $row[0] ?>">
<!--                                <form action="" method="POST">-->
                                    <input type="text" class="borderinput newcomment comment_<?php echo $row[0] ?>" postid="<?php echo $row[0] ?>" placeholder="Viết bình luận của bạn..." style="width:422px;height:20px"/> 
<!--                                </form>-->
                            </li>
                        </ul>
                    </div>
                </div>
            <?php } ?>
        </div>
    </div>
    <?php include_once 'include/rightsidebar.php'; ?>
</div>
<script type="text/javascript">
    if (document.getElementById("newpost")) {
        document.getElementById("newpost").onkeydown = function(e) {
            e = e || window.event;
            if (e.keyCode == 13) {
                if($("#newpost").val()!= ""){
                    $.ajax({
                        url:"controllers/postcontroller.php?action=addnewpost",
                        type:"POST",
                        data:$("#form-post").serialize(),
                        success:function(data){
                            if(data == true){
                                window.location = window.location;
                            }
                        }                           
                    });
                }
                return false;
            }
        };
    }
    $(function(){
        $(".delete_comment").live('click',function(){
            var id = $(this).attr("id");
            if(confirm("Bạn có chắc chắn muốn xóa comment này?")){
               $.ajax({
                url:"commentcontroller.php?action=delete_comment",
                type:"GET",
                data:{id:id},
                success:function(data){
                    if(data == true){
                        $(".cm"+id).fadeTo("slow", 0);
                        $(".cm"+id).slideUp("slow");
                    }
                }
            }); 
            }            
        });
        $(".delete_post").live('click',function(){
            var id = $(this).attr("pid");
            if(confirm("Bạn có chắc chắn muốn xóa chia sẻ này?")){
            $.ajax({
                url:"postcontroller.php?action=delete_post",
                type:"GET",
                data:{id:id},
                success:function(data){
                    if(data == true){
                        window.location = 'index.php';
                    }
                }
            });
            }
        });
        $(".newcomment").keydown(function(e){               
            e = e || window.event;            
            if (e.keyCode == 13) {
                var postid = $(this).attr("postid");
                if($(".comment_"+postid).val()!= ""){
                    $.ajax({
                        url:"commentcontroller.php?action=addnewcomment&postid="+postid,
                        type:"POST",
                        data:{content:$(".comment_"+postid).val()},
                        success:function(data){

                                $(".cm_"+postid).append(data);
                               //$(".cm_'"+postid+"' li:last").hide().slideDown("slow");
                               $(".comment-input_"+postid+" input").val("");
                        }                            
                    });
                }
            }
        })
    })
   
    /*if (document.getElementById("newpost")) {
            document.getElementById("newpost").onkeydown = function(e) {
                e = e || window.event;
                if (e.keyCode == 13) {
                    if($("#newpost").val()!= ""){
                        $.ajax({
                           url:"controllers/postcontroller.php?action=addnewpost",
                           type:"POST",
                           data:$("#form-post").serialize(),
                           success:function(data){
                             if(data == true){
                                 window.location = window.location;
                             }
                           }                           
                        });
                    }
                    return false;
                }
            };
        }*/
</script>

<?php include_once 'include/footer.php' ?>
